package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class TransferSeniorUnchainResultController extends TmsController {
	private Logger logger = Logger.getLogger(TransferSeniorUnchainResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			TmsUserProfile profile = uc.getUserProfile();
			uc.setAttribute("serial", view.getSerial());
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(view.getSerial(),"");
			accept.setSerial(view.getSerial());
//			accept.setStatus(13);
			int waitStatus = accept.getWaitForStatus();
			accept.setWaitForStatus(13);			
			accept.setExtendedPrice(Integer.parseInt(StringUtil.removeDH(view.getOutTimePrice())));
			accept.setUpdateUserId(profile.getUniqueId());
			accept.updateTransExtendPriceByEntity(accept);

			// 历史记录
			TransferProcessHistory history = new TransferProcessHistory();
			history.setSerial(view.getSerial());
			if(waitStatus == 14) 
			{
				history.setBusinessType(15);
			}else{
				history.setBusinessType(18);
			}
			history.setInsertUserId(profile.getUniqueId());
			history.setReason("");
			history.insertHisInfoAtAssess(history);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

}
